package com.guet.niit.Login.dao;

import com.guet.niit.Login.pojo.Agent;
import com.guet.niit.Login.pojo.People;
import com.guet.niit.Login.pojo.User;
import com.guet.niit.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class LoginDaoImpl implements LoginDao {
    //查询common_user表获取用户信息
    @Override
    public List<User> userCheckLogin() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        User login = null;
        List<User> logins = new ArrayList<>();

        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "select * from common_user";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                //获取数据
                long id = rs.getLong("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String photo = rs.getString("photo");
                //封装login对象
                login = new User(id,username,password,photo);
                //装载集合
                logins.add(login);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return logins;
    }

    //查询agent_user表获取代理商信息
    @Override
    public List<Agent> agentCheckLogin() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Agent login = null;
        List<Agent> logins = new ArrayList<>();

        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "select * from agent_user";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                //获取数据
                long id = rs.getLong("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String photo = rs.getString("photo");
                //封装login对象
                login = new Agent(id,username,password,photo);
                //装载集合
                logins.add(login);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return logins;
    }

    //新增common_user表的记录，注册普通用户
    @Override
    public int userRegister(User user) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "insert into common_user(id,username,password) values (?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setLong(1, user.getId());
            pstmt.setString(2,user.getUsername());
            pstmt.setString(3,user.getPassword());
            //执行sql
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return 1;
    }

    //新增agent_user表的记录，注册代理商
    @Override
    public int agentRegister(Agent agent) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "insert into agent_user(id,username,password) values (?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setLong(1, agent.getId());
            pstmt.setString(2,agent.getUsername());
            pstmt.setString(3,agent.getPassword());
            //执行sql
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return 1;
    }
}
